home *** CD-ROM | disk | FTP | other *** search
- ===============================================================================
- = Displays all prime numbers within a given range. Not very useful in itself,=
- = but it's a good example of using simple math and control flow commands. =
- ===============================================================================
-
- ** Display syntax message if we weren't given any arguments **
-
- ifeq @arg1 ""
- print "Proper usage is: UPL @arg0 firstnumber lastnumber"
- print
- print " where 'firstnumber' is the first number to be checked for being prime,"
- print " and 'lastnumber' is the last number to be checked."
- abort
- endif
- end
- run
-
- ** Initialize values for loop **
-
- set @var0 @arg1 Initialize test value
- ifle @var0 2 2 is a special case;
- print 2$09; force printing so
- set @var0 3 we can test only odd
- goto GotOne numbers in the loop
- endif
- set @var9 @var0 If starting value
- mod @var9 2 is an even number,
- ifeq @var9 0 it can't be prime, so
- add @var0 1 start with next odd
- endif
- set @var1 3 Initialize to first potential factor
-
- ** Test each number within the range by making sure it isn't evenly **
- ** divisible by any integer from 3 through its square root **
-
- Check4Factors:
- set @var9 @var0
- mod @var9 @var1
- ifeq @var9 0 If (VALUE MOD FACTOR)=0,
- goto CheckNext this is not a prime, reject it
- else
- set @var9 @var0
- div @var9 @var1
- ifgt @var1 @var9 If FACTOR > (VALUE/FACTOR),
- GotOne: we've tested all potential factors through
- print @var0$09; the square root without finding a valid one
- else Else we haven't tested all possible factors,
- add @var1 1 so try the next one
- goto Check4Factors
- endif
- endif
- CheckNext:
- add @var0 2 Test the next odd number
- ifle @var0 @arg2 If next number is within range,
- set @var1 3 reset to first potential factor
- goto Check4Factors and perform test
- endif
- end
-